package com.probox.modules.controller.platfom;

import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.PublishStatusEnum;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.platfom.PlatformInfoTemplate;
import com.probox.common.entity.domain.platfom.PlatformTemplate;
import com.probox.common.entity.domain.platfom.vo.PlatformTemplateVo;
import com.probox.common.entity.domain.platfom.vo.api.PlatformTemplateView;
import com.probox.common.entity.service.platfom.IPlatformInfoTemplateService;
import com.probox.common.entity.service.platfom.IPlatformTemplateService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 平台模板Controller
 *
 * @date 2021-06-22
 */
@RestController
@RequestMapping("/platform/template")
public class PlatformTemplateController extends BaseController {
    @Autowired
    private IPlatformTemplateService platformTemplateService;

    @Autowired
    private IPlatformInfoTemplateService platformInfoTemplateService;

    /**
     * 查询平台模板列表
     */
    @PreAuthorize(hasPermi = "platform:template:list")
    @GetMapping("/list")
    public TableDataInfo list(PlatformTemplate platformTemplate) {
        List<PlatformTemplateVo> list = new ArrayList<>();
        PlatformInfoTemplate platformInfoTemplate = new PlatformInfoTemplate();
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            platformInfoTemplate.setDataStatus(DataStatus.valid.getKey());
            platformTemplate.setPublishStatus(PublishStatusEnum.publish.getKey());
        }
        platformInfoTemplate.setPlatformUid(1L);
        Integer size = 0;
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryListSs(platformInfoTemplate);
        if (ObjectUtil.isNotEmpty(platformInfoTemplates)) {
            Long[] uids = new Long[platformInfoTemplates.size()];
            AtomicReference<Integer> index = new AtomicReference<>(0);
            platformInfoTemplates.forEach(vo -> {
                uids[index.get()] = vo.getTemplateUid();
                index.getAndSet(index.get() + 1);
            });
            startPage();
            list = platformTemplateService.queryList(platformTemplate, uids);
            List<PlatformTemplateVo> platformTemplateVos = platformTemplateService.queryList(platformTemplate, uids);
            if(ObjectUtil.isNotEmpty(platformTemplateVos)){
                size = platformTemplateVos.size();
            }

        }
        return getDataTable(list, size);
    }

    /**
     * 查询我的平台模板列表
     */
    @PreAuthorize(hasPermi = "platform:template:list")
    @GetMapping("/myList")
    public TableDataInfo listMy(PlatformTemplate platformTemplate) {
        List<PlatformTemplateVo> list = new ArrayList<>();
        PlatformInfoTemplate platformInfoTemplate = new PlatformInfoTemplate();
        platformInfoTemplate.setDataStatus(DataStatus.invalid.getKey());
        platformInfoTemplate.setPlatformUid(SecurityUtils.getPlatformId());
        platformTemplate.setPublishStatus(PublishStatusEnum.draft.getKey());
        List<PlatformInfoTemplate> platformInfoTemplates = platformInfoTemplateService.queryListSs(platformInfoTemplate);
        if (ObjectUtil.isNotEmpty(platformInfoTemplates)) {
            Long[] uids = new Long[platformInfoTemplates.size()];
            AtomicReference<Integer> index = new AtomicReference<>(0);
            platformInfoTemplates.forEach(vo -> {
                uids[index.get()] = vo.getTemplateUid();
                index.getAndSet(index.get() + 1);
            });
            startPage();
            list = platformTemplateService.queryList(platformTemplate, uids);
        }
        return getDataTable(list, platformInfoTemplates.size());
    }


    /**
     * 获取平台模板详细信息
     */
    @PreAuthorize(hasPermi = "platform:template:query")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId) {
        return AjaxResult.success(platformTemplateService.selectPlatformTemplateById(templateId));
    }


    @PostMapping("/saveOpenScreen")
    public AjaxResult saveOpenScreen(@RequestBody PlatformTemplate template){
        return AjaxResult.success(platformTemplateService.saveOpenScreen(template));
    }

    @GetMapping("/getOpenScreen")
    public AjaxResult getOpenScreen(){
        return AjaxResult.success(platformTemplateService.getOpenScreen());
    }

    /**
     * 新增平台模板
     */
    @Log(title = "平台模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @PreAuthorize(hasPermi = "platform:template:add")
    public AjaxResult add(@RequestBody PlatformTemplateView platformTemplateView) {
        return AjaxResult.success(platformTemplateService.insertPlatformTemplate(platformTemplateView));
    }

    /**
     * 使用平台模板
     *
     * @param platformTemplateView
     * @return
     */
    @Log(title = "平台模板", businessType = BusinessType.INSERT)
    @PostMapping("/use")
    public AjaxResult use(@RequestBody PlatformTemplateView platformTemplateView) {
        return AjaxResult.success(platformTemplateService.use(platformTemplateView.getTemplateId()));
    }

    /**
     * 删除平台模板
     */
    @PreAuthorize(hasPermi = "platform:template:remove")
    @Log(title = "平台模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long templateIds) {
        return toAjax(platformTemplateService.deletePlatformTemplateByIds(templateIds));
    }

    /**
     * 下架平台模板
     */
    @PreAuthorize(hasPermi = "platform:template:edit")
    @Log(title = "平台模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlatformTemplate platformTemplate) {
        return toAjax(platformTemplateService.updatePlatformTemplate(platformTemplate));
    }


    /**
     * 下架平台模板
     */
    @PreAuthorize(hasPermi = "platform:template:edit")
    @Log(title = "平台模板", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edits(@RequestBody PlatformTemplate platformTemplate) {
        return toAjax(platformTemplateService.updatePlatformTemplates(platformTemplate));
    }



}
